//
//  YJDUserBtn.m
//  YJDSDK
//
//  Created by skyline on 2018/3/6.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "YJDUserBtn.h"
#import "YJDUserViewController.h"

CGFloat const USERBTNGAP = 20;
@interface YJDUserBtn()
{
    CGPoint startPoint;
    CGPoint movePoint;
}
@property (nonatomic, strong) YJDImageView *imageView;

@end
@implementation YJDUserBtn
static YJDUserBtn *_userBtn;
static dispatch_once_t once_user;
+ (instancetype)yjd_shareUserBtn
{
    if (!_userBtn)
    {
        dispatch_once(&once_user, ^{
            _userBtn  = [[YJDUserBtn alloc] init];
            _userBtn.hidden = YES;
            _userBtn.imageView.hidden = NO;
        });
    }
    return _userBtn;
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.bounds = CGRectMake(0, 0, 50, 50);
        self.backgroundColor = [YJDHelper shareHelp].color;
        self.layer.cornerRadius = self.bounds.size.width / 2.0;
        self.layer.masksToBounds = YES;
        [self addTarget:self action:@selector(yjd_actionForUserBtn) forControlEvents:UIControlEventTouchUpInside];
        
        //悬浮于窗口,[window addSubView self]
        //固定在首页,[window.rootViewController.view addSubview:self]
        UIWindow *window = [UIApplication sharedApplication].delegate.window;
        if (window.rootViewController)
        {
            [window.rootViewController.view addSubview:self];
            [self mas_makeConstraints:^(SK_MASConstraintMaker *make) {
                make.right.equalTo(window.rootViewController.view).with.offset(-USERBTNGAP);
                make.top.equalTo(window.rootViewController.view).with.offset(USERBTNGAP);
                make.size.mas_equalTo(self.bounds.size);
            }];
        }
    }
    return self;
}

- (YJDImageView *)imageView
{
    if (!_imageView)
    {
       _imageView = [[YJDImageView alloc] initWithImageName:@"portrait"];
        [self addSubview:_imageView];
        [_imageView mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsZero);
        }];
    }
    return _imageView;
}

- (void)yjd_addlong
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(yjd_actionForLong:)];
    longPress.minimumPressDuration = 0.5;
    [self addGestureRecognizer:longPress];
}

- (void)yjd_actionForUserBtn
{
    if ([[YJDPlatform platform].accountID length] > 0 && [[YJDPlatform platform].token length] > 0)
    {
        YJDUserViewController *userVC = [[YJDUserViewController alloc] init];
        userVC.delegate = self.delegate;
        UIWindow *window = [UIApplication sharedApplication].delegate.window;
        userVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [window.rootViewController presentViewController:userVC animated:YES completion:nil];
    }
    else
    {
        [YJDPlatform prepareLogin];
    }
}

- (void)yjd_actionForLong:(UILongPressGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan)
    {
        startPoint = [sender locationInView:self];
    }
    else if(sender.state == UIGestureRecognizerStateChanged)
    {
        CGPoint point = [sender locationInView:self];
        movePoint = CGPointMake(point.x - startPoint.x, point.y - startPoint.y);
        self.transform = CGAffineTransformTranslate(self.transform, movePoint.x  , movePoint.y);
        
        //大方向--横向滑动
        if (fabs(movePoint.x)>fabs(movePoint.y))
        {
            //向右滑动
            if (movePoint.x>0)
            {
            }
            //向左滑动
            else if(movePoint.x<0)
            {
            }
            
        }
        //大方向--纵向滑动
        else if (fabs(movePoint.x)<fabs(movePoint.y))
        {
            //向下滑动
            if (movePoint.y >0)
            {
            }
            //向上滑动
            else if (movePoint.y<0)
            {
            }
        }
        
    }
    else if (sender.state == UIGestureRecognizerStateEnded)
    {
        self.alpha = 1.0;
        //原点，没有滑动
        if (movePoint.x == 0 && movePoint.y == 0)
        {
            [self performSelector:@selector(yjd_actionForUserBtn) withObject:self];
        }
        //滑动
        else
        {
            UIWindow *window = [UIApplication sharedApplication].delegate.window;
            CGSize size = window.bounds.size;
            CGPoint pointWindow = [sender locationInView:self.superview]; //触点在当前屏幕的坐标
            CGPoint  pointSelf = [sender locationInView:self]; //触点在本身的坐标
            CGFloat offsetX_MAX = size.width - pointWindow.x;  //终点到屏幕右边的距离
            CGFloat offsetX_MIN = pointWindow.x;               //终点到屏幕左边的距离
            CGFloat offsetY_MAX = size.height - pointWindow.y; //终点到屏幕下边的距离
            CGFloat offsetY_MIN = pointWindow.y;               //终点到屏幕上边的距离
            CGFloat toRight;
            CGFloat toTop;
            
            //判断，终点靠近屏幕右边
            if (MIN(offsetX_MAX, offsetX_MIN) == offsetX_MAX && MIN(offsetX_MAX, offsetX_MIN) < MIN(offsetY_MAX, offsetY_MIN))
            {
                DeLog(@"终点靠近屏幕右边");
                self.transform = CGAffineTransformTranslate(self.transform, offsetX_MAX- USERBTNGAP, 0);
                toRight = - USERBTNGAP;
                toTop = pointWindow.y + pointSelf.y - USERBTNGAP;
            }
            //终点靠近屏幕左边
            else if (MIN(offsetX_MAX, offsetX_MIN) == offsetX_MIN && MIN(offsetX_MAX, offsetX_MIN) < MIN(offsetY_MAX, offsetY_MIN))
            {
                DeLog(@"终点靠近屏幕左边");
                self.transform = CGAffineTransformTranslate(self.transform, - offsetX_MIN + USERBTNGAP, 0);
                toRight = - (size.width - USERBTNGAP - CGRectGetWidth(self.bounds));
                toTop = pointWindow.y + pointSelf.y - USERBTNGAP;
            }
            //终点靠近屏幕下边
            else if (MIN(offsetY_MAX, offsetY_MIN) == offsetY_MAX && MIN(offsetX_MAX, offsetX_MIN) > MIN(offsetY_MAX, offsetY_MIN))
            {
                DeLog(@"终点靠近屏幕下边");
                self.transform = CGAffineTransformTranslate(self.transform, 0, offsetY_MAX - USERBTNGAP);
                toRight = - (size.width - pointWindow.x - USERBTNGAP);
                toTop = size.height - USERBTNGAP;
            }
            //终点靠近屏幕上边
            else if (MIN(offsetY_MAX, offsetY_MIN) == offsetY_MIN && MIN(offsetX_MAX, offsetX_MIN) > MIN(offsetY_MAX, offsetY_MIN))
            {
                DeLog(@"终点靠近屏幕上边");
                self.transform = CGAffineTransformTranslate(self.transform, 0, - offsetY_MIN + USERBTNGAP);
                toRight = - (size.width - pointWindow.x - USERBTNGAP);
                toTop = USERBTNGAP;
            }
            else{}
            //            [self mas_updateConstraints:^(SK_MASConstraintMaker *make) {
            //                make.right.equalTo(window.rootViewController.view).with.offset(toRight);
            //                make.top.equalTo(window.rootViewController.view).with.offset(toTop);
            //            }];
        }
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
